package data

/**
* @author aFun
* @data 2025/4/10 01:04
* @email: afunaaa222@163.com / 1030935903
* @address: Guangdong University of Technology, China
 */

import "camp_lucky_draw/server"

type FloorAndPos struct {
	Floor int8
	Pos   int8
}

var GetLayerCfgByFloor = make(map[int8]map[string]interface{})
var GetConstCfgById = make(map[int]interface{})
var GetFloorRewards = make(map[FloorAndPos]interface{})
var GetCumulativeRewards = make(map[int64]interface{})
var GetFloorMax int8

func Init_data() {
	init_get_layer_cfg_by_floor()
	init_get_const_cfg_by_id()
	init_floor_rewards()
	init_cumulative_rewards()
	init_floor_max()
}

func init_get_layer_cfg_by_floor() {
	LayerCfg := server.CampLuckyDrawData["layer_cfg"].([]interface{})
	for _, data := range LayerCfg {
		dataMap, _ := data.(map[string]interface{})
		// 这里有个坑：json 默认将数字解析为 float64 类型，如果断言成int将一直读到0
		floor, _ := dataMap["floor"].(float64)
		GetLayerCfgByFloor[int8(floor)] = dataMap
	}
}

func init_get_const_cfg_by_id() {
	ConstCfg := server.CampLuckyDrawData["const_cfg"].([]interface{})
	for _, data := range ConstCfg {
		dataMap, _ := data.(map[string]interface{})
		id, _ := dataMap["id"].(float64)
		// 逐一判断四个value值
		var value interface{}
		if dataMap["value"] != nil {
			value = dataMap["value"]
		} else if dataMap["value1"] != nil {
			value = dataMap["value1"]
		} else if dataMap["value2"] != nil {
			value = dataMap["value2"]
		} else if dataMap["value3"] != nil {
			value = dataMap["value3"]
		} else {
			panic("初始化常量配置失败，疑似参数有误")
		}
		GetConstCfgById[int(id)] = value
	}
}

func init_floor_rewards() {
	FloorRewards := server.CampLuckyDrawData["floor_rewards"].([]interface{})
	for _, FR := range FloorRewards {
		FRDict := FR.(map[string]interface{})
		FAP := FloorAndPos{int8(FRDict["floor"].(float64)), int8(FRDict["pos"].(float64))}
		GetFloorRewards[FAP] = FR
	}
}

func init_cumulative_rewards() {
	CumulativeRewards := server.CampLuckyDrawData["cumulative_rewards"].([]interface{})
	for _, CR := range CumulativeRewards {
		CRDict := CR.(map[string]interface{})
		Index := int64(CRDict["index"].(float64))
		GetCumulativeRewards[Index] = CR
	}
}

func init_floor_max() {
	for k, _ := range GetFloorRewards {
		GetFloorMax = max(GetFloorMax, k.Floor)
	}
}
